package com.yiren.dbaa.module.practice_set.map.presenter;

import android.app.Activity;
import android.content.DialogInterface;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.basestation.bean.BaseStationBean;
import com.yiren.dbaa.module.practice_set.map.contract.AddMapContract;
import com.yiren.dbaa.module.practice_set.map.contract.MapHomeContract;
import com.yiren.dbaa.module.practice_set.map.model.AddMapModel;
import com.yiren.dbaa.module.practice_set.map.model.MapHomeModel;
import com.yiren.dbaa.module.practice_set.site.bean.CityBean;
import com.yiren.dbaa.module.practice_set.site.bean.ProvinceBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.model.AddSiteRuleModel;
import com.yiren.dbaa.module.practice_set.site.model.SiteRuleModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/28 18:24
 * @description: 新增地图
 */
public class AddMapPresenter implements AddMapContract.Presenter {

    private Activity activity;
    private AddMapContract.Model addMapModel;
    private MapHomeContract.Model mapHomeModel;
    private SiteRuleContract.Model siteRuleModel;
    private AddSiteRuleContract.Model addSiteModel;
    private UserParamConfigBean userParamConfig;

    public AddMapPresenter(Activity activity) {
        this.activity = activity;
        addMapModel = new AddMapModel(activity);
        mapHomeModel = new MapHomeModel(activity);
        siteRuleModel = new SiteRuleModel(activity);
        addSiteModel = new AddSiteRuleModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadShareMap(AddMapContract.View view, String city, String user) {
        LoadDialog.show(activity, "正在查询中...");
        addMapModel.getShareMap(city, user, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<MapEntity> mapList = (List<MapEntity>) result;
                view.showShareMapList(mapList);
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void loadProvinceCity(AddMapContract.View view) {
        activity.runOnUiThread(() -> LoadDialog.show(activity, "数据正在加载中..."));
        addSiteModel.getProvince(new CommonCallBack() {
            @Override
            public void success(Object result) {
                List<ProvinceBean> provinceBeanList = (List<ProvinceBean>) result;
                for (int i = 0; i < provinceBeanList.size(); i++) {
                    final int k = i;
                    ProvinceBean province = provinceBeanList.get(i);
                    addSiteModel.getCity(province.getId(), new CommonCallBack() {
                        @Override
                        public void success(Object result) {
                            province.setCityBeanList((List<CityBean>) result);
                            provinceBeanList.set(k, province);
                            if (k == 33) {
                                view.showProvinceCity(provinceBeanList);
                            }
                        }
                    });
                }
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void addMapCollect(AddMapContract.View view) {
        LoadDialog.show(activity, "正在获取基站，请稍后...");
        addMapModel.getBaseStationList(new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<BaseStationBean> baseStationBeans = (List<BaseStationBean>) result;
                if (null == baseStationBeans && baseStationBeans.size() == 0) {
                    LoadDialog.dismiss(activity);
                    MessageBox.show(activity, "没有可选的基站");
                } else {
                    siteRuleModel.getSiteRuleFromServer(userParamConfig.getUserInfo().getId().toString(), false, new CommonCallBack() {
                        @Override
                        public void success(Object result) {
                            List<SiteEntity> siteList = (List<SiteEntity>) result;
                            if (null != siteList && siteList.size() > 0) {
                                mapHomeModel.getMapFromServer(userParamConfig.getUserInfo().getId().toString(), false, new CommonCallBack() {
                                    @Override
                                    public void success(Object result) {
                                        List<MapEntity> mapList = (List<MapEntity>) result;
                                        //获取本地列表
                                        List<MapEntity> localMaps = UserDbController.getInstance(false).queryAll(MapEntity.class);
                                        //数据合并去重，服务器与本地数据都存在以本地数据为准
                                        if (null != localMaps && localMaps.size() > 0) {
                                            mapList = mapHomeModel.lineHomeDataMerge(localMaps, mapList);
                                        }
                                        if (null != mapList && mapList.size() > 0) {
                                            for (int i = 0; i < mapList.size(); i++) {
                                                for (int j = 0; j < siteList.size(); j++) {
                                                    if (siteList.get(j).getSiteCode().equals(mapList.get(i).getSiteCode())) {
                                                        siteList.remove(j);
                                                        break;
                                                    }
                                                }
                                            }
                                            view.showAddMapDialog(userParamConfig.getUserInfo().getId(), siteList, baseStationBeans);
                                        } else {
                                            view.showAddMapDialog(userParamConfig.getUserInfo().getId(), siteList, baseStationBeans);
                                        }
                                    }

                                    @Override
                                    public void fail(String error) {
                                        MessageBox.show(activity, error);
                                    }
                                });
                            } else {
                                view.showAddMapDialog(userParamConfig.getUserInfo().getId(), siteList, baseStationBeans);
                            }
                        }

                        @Override
                        public void fail(String error) {
                            MessageBox.show(activity, error);
                        }
                    });
                }
            }
        });

    }

    @Override
    public void downLoadMap(MapEntity map,List<MapEntity> mapListSer, CommonCallBack callBack) {
        //判断数据是否存在
        boolean isHaveData = addMapModel.isHaveData(map,mapListSer);
        if (isHaveData) {
            CommonDialog.showTwoBtn(activity, "操作确认", "您已存在" + map.getName() + "，下载将会覆盖!\n确定覆盖下载吗?", "取消", "覆盖下载", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        mapHomeModel.downloadSiteMap(map.getSiteCode(), userParamConfig.getUserInfo().getpUserId(), map.getMapVer(), new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                LoadDialog.dismiss(activity);
                                if (UserDbController.getInstance(false).insertOrReplace(map)) {
                                    //通知视图
                                    LoadDialog.dismiss(activity);
                                    MessageBox.MessageAndVoiceModel(activity, "下载地图成功");
                                    callBack.success("");
                                } else {
                                    MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                                }
                            }

                            @Override
                            public void fail(String error) {
                                MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                                MessageBox.show(activity, error);
                            }
                        });
                        dialog.dismiss();
                    }
                }
            });

        } else {
            mapHomeModel.downloadSiteMap(map.getSiteCode(), userParamConfig.getUserInfo().getpUserId(), map.getMapVer(), new CommonCallBack() {
                @Override
                public void success(Object result) {
                    LoadDialog.dismiss(activity);
                    if (UserDbController.getInstance(false).insertOrReplace(map)) {
                        //通知视图
                        LoadDialog.dismiss(activity);
                        MessageBox.MessageAndVoiceModel(activity, "下载地图成功");
                        callBack.success("");
                    } else {
                        MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                    }
                }

                @Override
                public void fail(String error) {
                    MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                    MessageBox.show(activity, error);
                }
            });

        }

    }
}
